﻿@{
    ViewBag.Title = "明细账";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<link href="~/Content/css/warehouse_ledger_details.css" rel="stylesheet" />
<link href="~/Content/css/warehouse_ledger_details_print.css" rel="stylesheet" media="print" />


<script type="text/javascript">
    $(function () {
        Index.init();
    });

    var Index = {
        jsonData:@Html.Raw(ViewData["jsonData"].ToString()),
        listData:@Html.Raw(ViewData["listData"].ToString()),
        sql:'@ViewData["sql"].ToString()',
        curUserName:'@ViewData["curUserName"].ToString()',
        curDate:'@ViewData["curDate"].ToString()',
        curTime:'@ViewData["curTime"].ToString()', 
        printItemTemplate:"",
        init: function () {
            with (Index) {
                printItemTemplate = $("#print .page").html();

                initTable();

                initControl();

                initData();
            }
        },
        initTable: function () {
            $(".table_c table").delegate("td", "click", function () {
                var len = $(this).parents(".selected").length;

                0!=len && (function(cur){
                    cur.parents("tr").removeClass("selected");
                })($(this));

                0==len && (function(cur){
                    cur.parents("tr").addClass("selected");

                    cur.parents("tr").siblings().removeClass("selected");
                })($(this));
            });
        },
        initControl: function () {
            $("#butnReturn").on("click", function () {
                history.back(-1);
            });

            $("#butnPrint").on("click",function(){
                $("#print").jqprint();
            });

            $("#butnExport").on("click",function(){
                var param = $.param({sql:Index.sql});

                var url = "/Common/ExcelExport?{0}".format(param);

                location.href=url;
            });

        },
        initData:function(){
            var colunms = "零件名称,零件编号,Fprice_X,FLowestStore,制造单位名称,零件分类".split(",");

            colunms.forEach(function(cur){
                $("[name={0}]".format(cur)).val(Index.jsonData[cur]);
            });

            var tmp = Index.jsonData["FAccountDate"],date="";

            date={year:tmp.substr(0,4),month:tmp.substr(4,2)};

            $("h1").text("{year}年{month}月仓库收发存明细账页".format(date));

            var exclude = "发生单位,FDocuTypeNO,FQuantity_QC,来料单位,来料单位名称,入库单号".split(",");

            Index.renderData($("#dataList"),Index.listData,exclude);

            Index.renderPrintData($("#print"),Index.listData,exclude);
        },
        renderData:function(c,data,hiddenCols){
            c.empty();

            var colunms = [];
            
            data.forEach(function(cur){
                !colunms.length && (function(){
                    var title = $("<tr>");

                    for(var idx in cur){
                        0 <= hiddenCols.indexOf(idx) || (function(){
                            colunms.push(idx);
                            
                            $("<th>").text(idx).appendTo(title);
                        })();
                    }

                    title.appendTo(c);
                })();

                !!colunms.length && (function(){
                    var row = $("<tr>");

                    row.attr("rowData",JSON.stringify(cur));

                    for(var idx in colunms){
                        $("<td>").text(cur[colunms[idx]]).appendTo(row);
                    }

                    row.appendTo(c);
                })();
            });
        },
        renderPrintData:function(c,data,hiddenCols){
            c.empty();
            
            var rows = 16;
            // 零件编号 核算单价 最高储备 最低储备 制造单位 零件分类
            //零件名称,零件编号,Fprice_X,FLowestStore,制造单位名称,零件分类
            var item = {
                "零件名称":Index.jsonData["零件名称"],
                "零件编号":Index.jsonData["零件编号"],
                "核算单价":Index.jsonData["单价 （元）"],
                "最高储备":(Index.jsonData["最高储备"]?Index.jsonData["最高储备"]:""),
                "最低储备":(Index.jsonData["最低储备"]?Index.jsonData["最低储备"]:""),
                "制造单位":Index.jsonData["制造单位"],
                "零件分类":Index.jsonData["零件分类"],
                page:1,maxPage:1,title:$("#wrapper h1").text() ,
                user:Index.curUserName,
                date:Index.curDate,
                time:Index.curTime,
                table:null
            };

            var max = data.length;

            var maxPage = Math.ceil(max/rows);

            item.maxPage = maxPage;

            for(var i = 0;i < maxPage;i++){
                var colunms = [];

                var start = i*rows,end=(i+1)*rows;

                var table = $("<table>"),pageItem = $("<div>").addClass("page");

                end>max && (end=max);

                item.page = i+1;
                
                for(var j = start;j<end; j++){
                    var cur = data[j];

                    !colunms.length && (function(){
                        var title = $("<tr>");

                        for(var idx in cur){
                            0 <= hiddenCols.indexOf(idx) || (function(){
                                colunms.push(idx);
                            
                                $("<th>").text(idx).appendTo(title);
                            })();
                        }

                        title.appendTo(table);
                    })();

                    !!colunms.length && (function(){
                        var row = $("<tr>");

                        row.attr("rowData",JSON.stringify(cur));

                        for(var idx in colunms){
                            $("<td>").text(cur[colunms[idx]]).appendTo(row);
                        }

                        row.appendTo(table);
                    })();

                }
                
                item.table = "<table>{0}</table>".format(table.html());

                pageItem.html(Index.printItemTemplate.format(item)).appendTo(c);
            }
        },
        initSize:function(){
            var resize = function (p) {
                var width = p.w.width();

                p.b.css({ "font-size": "{0}px".format(width / 160) });
            }

            resize({ b: $("body"), w: $(window) });

            $(window).resize(function () {
                resize({ b: $("body"), w: $(window) });
            });
        }


    };

</script>
<body>
    <div id="wrapper">
        <h1>2017年03月仓库收发存明细账页</h1>
        <header>
            <div class="f_l">
                <div class="input_area">
                    <h2 class="input_title">品名</h2>
                    <div class="input_c input_c_w20">
                        <input name="零件名称" type="text" class="txt_common" readonly  value='@ViewData["零件名称"].ToString()'/>
                    </div>
                </div>
                <div class="input_area">
                    <h2 class="input_title">图号规格</h2>
                    <div class="input_c input_c_w15">
                        <input name="零件编号" type="text" class="txt_common" readonly  value='@ViewData["零件名称"].ToString()'/>
                    </div>
                </div>
                <div class="input_area input_area_w">
                    <h2 class="input_title">核算单价</h2>
                    <div class="input_c input_c_w12">
                        <input name="Fprice_X" type="text" class="txt_common_r" readonly  value='@ViewData["Fprice_X"].ToString()'/>
                    </div>
                    <h2 class="input_title_after">元</h2>
                </div>
                <div class="input_area input_area_w">
                    <h2 class="input_title">最高储备</h2>
                    <div class="input_c input_c_w12">
                        <input name="FHighestStore" type="text" class="txt_common_r" readonly   value='@ViewData["FHighestStore"].ToString()'/>
                    </div>
                    <h2 class="input_title_after">件</h2>
                </div>
                <div class="input_area input_area_w">
                    <h2 class="input_title">最低储备</h2>
                    <div class="input_c input_c_w12 ">
                        <input name="FLowestStore" type="text" class="txt_common_r" readonly   value='@ViewData["FLowestStore"].ToString()'/>
                    </div>
                    <h2 class="input_title_after">件</h2>
                </div>
                <div class="input_area">
                    <h2 class="input_title">制造单位</h2>
                    <div class="input_c input_c_w20">
                        <input name="制造单位名称" type="text" class="txt_common" readonly value='@ViewData["制造单位名称"].ToString()' />
                    </div>
                </div>
                <div class="input_area">
                    <h2 class="input_title">零件分类</h2>
                    <div class="input_c input_c_w15">
                        <input name="零件分类" type="text" class="txt_common" readonly  value='@ViewData["零件分类"].ToString()'/>
                    </div>
                </div>
            </div>
            <div class="f_r">
                <button id="butnPrint" class="butn_common">
                    <span class="glyphicon glyphicon-print"></span>打印
                </button>
                <button id="butnExport" class="butn_common">
                    <span class="glyphicon glyphicon-export"></span>导出
                </button>
                <button id="butnReturn" class="butn_common">
                    <span class="glyphicon glyphicon-circle-arrow-left"></span>返回
                </button>
            </div>
        </header>
        <div class="table_c">
            <table id="dataList">
                <tr>
                    <td>正在加载数据。。。</td>
                </tr>
            </table>
        </div>
        <div class="page_view">
            <div class="page_total">显示第 1 - 1 条记录　检索到 1 条记录</div>
            <div class="pages">
                <a href="javascript:void(0);" class="page _bg_bg_page_01">首页</a>
                <a href="javascript:void(0);" class="page _bg_bg_page_02">上页</a>
                <input type="text" class="txt_page" value="1" />
                <div class="show_page">共一页</div>
                <a href="javascript:void(0);" class="page _bg_bg_page_03">下页</a>
                <a href="javascript:void(0);" class="page _bg_bg_page_04">末页</a>
                <select class="sel_page">
                    <option>50</option>
                    <option>150</option>
                    <option>200</option>
                </select>
            </div>
        </div>
    </div>
    <div id="print">
        <div class="page">
            <h1>{title}</h1>
            <header>
                <div class="input_area">
                    <h2 class="input_title">品名</h2>
                    <div class="input_c input_c_w20">
                        {零件名称}
                    </div>
                </div>
                <div class="input_area">
                    <h2 class="input_title">图号规格</h2>
                    <div class="input_c  input_c_w15">
                        {零件编号}
                    </div>
                </div>
                <div class="input_area">
                    <h2 class="input_title">核算单价</h2>
                    <div class="input_c input_c_w12">
                        {核算单价}
                    </div>
                    <h2 class="input_title_after">元</h2>
                </div>
                <div class="input_area">
                    <h2 class="input_title">最高储备</h2>
                    <div class="input_c input_c_w12">
                        {最高储备}
                    </div>
                    <h2 class="input_title_after">件</h2>
                </div>
                <div class="input_area">
                    <h2 class="input_title">最低储备</h2>
                    <div class="input_c input_c_w12">
                        {最低储备}
                    </div>
                    <h2 class="input_title_after">件</h2>
                </div>
                <div class="input_area">
                    <h2 class="input_title">制造单位</h2>
                    <div class="input_c input_c_w12">
                        {制造单位}
                    </div>
                </div>
                <div class="input_area">
                    <h2 class="input_title">零件分类</h2>
                    <div class="input_c input_c_w15">
                        {零件分类}
                    </div>
                </div>
                <div class="right_page">第{page}页，共{maxPage}页</div>
            </header>
            <div class="table_c">
                {table}
            </div>
            <footer>
                <span>打印人员：<label>{user}</label></span> <span>打印日期：<label>{date}</label><label>{time}</label></span>
            </footer>
        </div>
    </div>
</body>
